package com.jd.rapid.rpc.client;

import com.jd.rapid.rpc.codec.RpcDecoder;
import com.jd.rapid.rpc.codec.RpcEncode;
import com.jd.rapid.rpc.codec.RpcRequest;
import com.jd.rapid.rpc.codec.RpcResponse;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

public class RpcClientInitializer extends ChannelInitializer<NioSocketChannel> {

    @Override
    protected void initChannel(NioSocketChannel socketChannel) throws Exception {
        //编解码的handler
        ChannelPipeline cp=socketChannel.pipeline();
        //client端对request编码
        cp.addLast(new RpcEncode(RpcRequest.class));
        //0-4是包头，解码辅助
        cp.addLast(new LengthFieldBasedFrameDecoder(65535,0,4,0,0));
        //client端对response解码
        cp.addLast(new RpcDecoder(RpcResponse.class));
        //实际的业务处理器
        cp.addLast(new RpcClientHandler());
    }
}
